package com.demo.study.structural.d07_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author Baymax
 * @since 2021/1/17
 */
public class WeaponProxy implements InvocationHandler {
    private Weapon weapon;

    public WeaponProxy(Weapon weapon) {
        this.weapon = weapon;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("检查是否有子弹");
        int bullet = weapon.getBullet();
        if (bullet == 0) {
            System.out.println("子弹为空，请上弹！");
            return null;
        } else {
            System.out.println("有" + weapon.getBullet() + "颗子弹，可以开枪");
        }
        Object invoke = method.invoke(weapon, args);
        System.out.println("子弹打完了");
        return invoke;
    }
}
